//
//  XYSDKManager.m
//  NemoSDKDemo
//
//  Created by 高超 on 2020/7/30.
//  Copyright © 2020 JackYang. All rights reserved.
//

#import "XYSDKManager.h"
#import <NemoSDK/NemoSDK.h>

#define RELEASEID @"12e53a6df2e91e6177e627c8e336a6888ff98104"
#define PRIVATEID @"3e816492058911e7a31d000c29971af5"
#define SERVER @"39.172.69.238:7443"

@interface XYSDKManager ()<NemoSDKDelegate>

@property (nonatomic, strong) NemoSDK *nemoSDK;

@end

@implementation XYSDKManager

+ (XYSDKManager *)sharedInstance {
    static XYSDKManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[XYSDKManager alloc] init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _isDefaultFrontCamera = 1; //默认前置摄像头
    }
    return self;
}

- (void)create:(NSString *)account password:(NSString *)password {
    XYSettings *settings = XYSettings.settings();
    settings.extID = PRIVATEID;
    settings.server = SERVER;
    settings.level = XYLogLevel_Debug;
    settings.enableLog = YES;
    settings.needAudioMeter = YES;
    settings.defaultCameraId = _isDefaultFrontCamera;
    settings.enableSSLCertificate = NO; //私有云3.9需要开启，默认NO
    XYVideoConfig *videoConfig = [[XYVideoConfig alloc] init];
    videoConfig.videoDimension = XYVideoDimension_720P;
    settings.videoConfig = videoConfig;
    
    _nemoSDK = [NemoSDK sharedInstance];
    [_nemoSDK shareNemoSessionSettings:settings];
    
    NSString *externalID = [[NSUserDefaults standardUserDefaults] objectForKey:@"externalID"];
    if (externalID == nil) {
        //初次创建生成随机id
        externalID = [NSString stringWithFormat:@"%06d",arc4random()%1000000];
        [[NSUserDefaults standardUserDefaults] setObject:externalID forKey:@"externalID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [_nemoSDK loginXYLinkAccount:account password:password];
}

- (void)joinMeeting:(NSString *)number password:(NSString *)password {
    [_nemoSDK makeCall:number password:password orientation:NemoVideoOrientation_Portrait];
    _meetingNumber = number;
}

- (void)setupVideo:(XYRTCVideoCanvas *)videoCanvas {
    [_nemoSDK setupVideo:videoCanvas];
}

- (void)updateVideo:(XYRTCVideoCanvas *)videoCanvas {
    [_nemoSDK updateVideo:videoCanvas];
}

- (void)setLayoutPolicy:(NemoCustomLayoutInfo *)layoutPolicys{
    [_nemoSDK setCustomLayout:layoutPolicys];
}

- (void)leaveMeeting {
    [_nemoSDK hangup:NemoHangUpMode_Default];
}

- (void)acceptMeeting {
    [_nemoSDK answer:_orientation];
}

- (void)rejectMeeting {
    [_nemoSDK hangup:_nemoSDK.isInMeeting?NemoHangUpMode_Invite:NemoHangUpMode_Default];
}

- (BOOL)isComingCallDispose {
    return _nemoSDK.isComingCallDispose;
}

- (void)dialDtmfWithfullScreenLayout:(NemoLayout *)fullScreenLayout content:(NSString *)content {
    if (fullScreenLayout == nil) {
        return;
    }
    if (fullScreenLayout.remoteID == 0) {
        return;
    }
    [_nemoSDK dialDtmfWithRemoteID:fullScreenLayout.remoteID dataType:fullScreenLayout.dataType content:content];
}

- (void)switchTorchMode:(NemoTorchMode)mode {
    [_nemoSDK switchTorchMode:mode];
}

- (NemoTorchMode)torchMode {
    return [_nemoSDK torchMode];
}

- (void)torchLevel:(float)level {
    [_nemoSDK torchLevel:level];
}

- (void)startRecording{
    [_nemoSDK startRecordingWithRecordingUrl:_recordingUrl];
    _isRecording = YES;
}

- (void)stopRecording{
    [_nemoSDK stopRecordingWithRecordingUrl:_recordingUrl];
    _isRecording = NO;
}

- (void)checkPermissionWithCompletion:(void (^)(BOOL isRecordable, NSString *recordingUrl, NSString *errorInfo))handler {
    
    [_nemoSDK checkPermissionWithMeetingNumber:_meetingNumber completion:^(BOOL isRecordable, NSString *recordingUrl, NSString *errorInfo) {
        _recordingUrl = recordingUrl;
        handler(isRecordable,recordingUrl, errorInfo);
    }];
}

- (void)checkSorageWithCompletion:(void (^)(BOOL isStorage,NSString *errorInfo))handler{
    [_nemoSDK checkSorageSpaceWithRecordingUrl:_recordingUrl
                                completion:^(BOOL isStorage, NSString *errorInfo) {
                                    handler(isStorage, errorInfo);
                                }];
}

- (NSString *)getMeetingControlUrl {
    return [_nemoSDK getMeetingHost];
}

- (void)setLocalMicMute:(BOOL)mute {
    [_nemoSDK enableAudioOfMic:!mute];
}

- (void)setSpeakerphoneOn:(BOOL)on {
    [_nemoSDK speakerOnModeDefault:!on];
}

- (BOOL)isLocalMicMuted {
    return [_nemoSDK isLocalMicMuted];
}

- (void)setLocalVideoMute:(BOOL)mute {
    [_nemoSDK enableVideo:!mute];
}

- (BOOL)isLocalVideoMuted {
    return [_nemoSDK isLocalVideoMuted];
}

- (void)handsUp {
    [_nemoSDK informManagerForRequestToSpeak];
}

- (void)handsDown {
    [_nemoSDK informManagerForCancelSpeak];
}

- (void)endSpeech {
    [_nemoSDK informManagerForEndSpeak];
}

- (void)enableAudioMode:(BOOL)enable {
    [_nemoSDK audioMode:enable];
}

- (void)switchCamera:(NemoDeviceType)type {
    [_nemoSDK switchCamera:type];
}

- (void)startContentSharing:(NemoShareContentType)type {
    [_nemoSDK startContentSharing:type];
}

- (void)sendContentMsg:(XYRTCContent *)content {
    [_nemoSDK sendContentMsg:content];
}

- (void)stopContentSharing {
    [_nemoSDK stopContentSharing];
}

- (void)changeOrientation:(NemoVideoOrientation)orientation {
    _orientation = orientation;
    [_nemoSDK changeVideoOrientation:orientation];
}

- (void)startWhiteboard{
    [_nemoSDK startWhiteboard];
    [XYLayoutManager sharedInstance].whiteboardState = XYWhiteboardState_Start;
}

- (void)stopWhiteboard{
    [_nemoSDK stopWhiteboard];
    [XYLayoutManager sharedInstance].whiteboardState = XYWhiteboardState_Stop;
}

- (void)sendWhiteboardMsg:(id)message type:(NemoWhiteboardOperationState)type{
    [_nemoSDK sendDataWhiteboard:message type:type];
}

- (void)modifyDisplayName:(NSString *)displayName completion:(void (^)(BOOL))completion {
    [_nemoSDK modifyDisplayName:displayName completion:^(BOOL success, id  _Nonnull responseObject) {
        if (completion) {
            completion(success);
        }
    }];
}

- (void)sendFeedbackLogWithComments:(NSString *)comments {
    [_nemoSDK sendFeedbackLogWithComments:comments];
}

- (void)farEndHardwareControlOperation:(XYFECCOperation)operation participantId:(NSString *)participantId {
    if (participantId.length == 0 || participantId == nil) {
        return;
    }
    BOOL isNumber = NO;
    NSScanner *scan = [NSScanner scannerWithString:participantId];
    int val;
    isNumber = [scan scanInt:&val] && [scan isAtEnd];
    if (isNumber) {
        [_nemoSDK farEndHardwareControl:participantId operation:operation];
    }else{
        NSLog(@"%s participantId is not available", __FUNCTION__);
    }
}

- (NSString *)sdkVersion {
    return [_nemoSDK version];
}

- (NSString *)getGeneralStatistics {
    return [_nemoSDK getGeneralStatistics];
}

- (NSString *)getDBStatistics {
    return [_nemoSDK getDBStatistics];
}

- (NSString *)getVideoTxStatistics {
    return [_nemoSDK getVideoTxStatistics];
}

- (NSString *)getVideoRxStatistics {
    return [_nemoSDK getVideoRxStatistics];
}

- (NSString *)getAudioTxStatistics {
    return [_nemoSDK getAudioTxStatistics];
}

- (NSString *)getAudioRxStatistics {
    return [_nemoSDK getAudioRxStatistics];
}

- (NSString *)getOtherStatistics {
    return [_nemoSDK getOtherStatistics];
}

- (NSString *)getLayoutStatistics {
    return [_nemoSDK getLayoutStatistics];
}

@end
